<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2018/6/21
 * Time: 下午5:08
 */


function success($message = '成功')
{
    $return = ['status' => 1];
    if ($message) {
        $return['message'] = $message;
    }

    return $return;
}

function error($message = '失败')
{
    $return = ['status' => 2];
    if ($message) {
        $return['message'] = $message;
    }

    return $return;
}


/**
 * 将日期转换为时间
 * @param $date
 * @return array
 */
function dateToArr($date)
{
    $year = substr($date, 0, 4);
    $month = substr($date, 5, 2);
    $day = substr($date, 8, 2);
    $hour = substr($date, 11, 2);
    $minute = substr($date, 14, 2);
    $second = substr($date, 17, 2);
    return compact('year', 'month', 'day', 'hour', 'minute', 'second');
}

function socket()
{
    set_time_limit(0); //限制执行时间  0为不限制
    $ip = '127.0.0.1';
    $port = 8001;//端口

    /**
     *
     * socket通信整个过程
     *
     * socket_create  //创建一个套接字
     *
     * socket_bind  //给套接字绑定 ip 和端口
     *
     * socket_listen //监听套接字上的连接
     *
     * socket_accept //接受一个socket连接
     *
     * socket_read //接收客户端 发送的数据
     *
     * socket_write //将数据写到 socket 缓存 向客户端发送
     *
     * socket_close   //关闭套接字资源
     */

    if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {
        echo "socket_create() 失败的原因是:" . socket_strerror($sock) . "\n";
    }

    if (($ret = socket_bind($sock, $ip, $port)) < 0) {
        echo "socket_bind() 失败的原因是:" . socket_strerror($ret) . "\n";
    }

    if (($ret = socket_listen($sock, 4)) < 0) {
        echo "socket_listen() 失败的原因是:" . socket_strerror($ret) . "\n";
    }
    $count = 0;
    do {
        if (($msgsock = socket_accept($sock)) < 0) {
            echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
            break;
        } else {
            //发到客户端
            $msg = "测试成功！\n";
            socket_write($msgsock, $msg, strlen($msg));
            echo "测试成功了啊\n";
            $buf = socket_read($msgsock, 8192);
            $talkback = "收到的信息:$buf\n";
            echo $talkback;
            if (++$count >= 5) {
                break;
            };
        }
        socket_close($msgsock);
    } while (true);
    socket_close($sock);
}

/**
 * 校验身份证
 * @param $id
 * @return bool
 */
function is_idcard($id)
{
    $id = strtoupper($id);
    $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
    $arr_split = array();
    if (!preg_match($regx, $id)) {
        return FALSE;
    }
    if (15 == strlen($id)) //检查15位
    {
        $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";

        @preg_match($regx, $id, $arr_split);
        //检查生日日期是否正确
        $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
        if (!strtotime($dtm_birth)) {
            return FALSE;
        } else {
            return TRUE;
        }
    } else      //检查18位
    {
        $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
        @preg_match($regx, $id, $arr_split);
        $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
        if (!strtotime($dtm_birth)) //检查生日日期是否正确
        {
            return FALSE;
        } else {
            //检验18位身份证的校验码是否正确。
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
            $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            $sign = 0;
            for ($i = 0; $i < 17; $i++) {
                $b = (int)$id{$i};
                $w = $arr_int[$i];
                $sign += $b * $w;
            }
            $n = $sign % 11;
            $val_num = $arr_ch[$n];
            if ($val_num != substr($id, 17, 1)) {
                return FALSE;
            } //phpfensi.com
            else {
                return TRUE;
            }
        }
    }

}


/**
 * 将日期转换为时间戳
 * @param $date
 * @return array
 */
function date2Str ($date)
{
    $time = explode(' - ', $date);
    $timefrom = $time[0];
    $timeto = $time[1];
    $from0 = explode('/', $timefrom);
    $from = $from0[2] . '-' . $from0[0] . '-' . $from0[1] . ' 00:00:00';
    $to0 = explode('/', $timeto);
    $to = $to0[2] . '-' . $to0[0] . '-' . $to0[1] . ' 23:59:59';
    return [strtotime($from), strtotime($to)];
}
function date2Date ($date)
{
    $time = explode(' - ', $date);
    $timefrom = $time[0];
    $timeto = $time[1];
    $from0 = explode('/', $timefrom);
    $from = $from0[2] . '-' . $from0[0] . '-' . $from0[1] . ' 00:00:00';
    $to0 = explode('/', $timeto);
    $to = $to0[2] . '-' . $to0[0] . '-' . $to0[1] . ' 23:59:59';
    return [$from, $to];
}
